//
//  LSWelcomeController.swift
//  MicroBlog
//
//  Created by lj on 15/9/6.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class LSWelcomeController: UIViewController {

    /// 头像底部约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        iconView.sd_setImageWithURL(LSUserAccountViewModel.sharedUserAccountViewModel.avatarUrl)
    
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //计算约束数值
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
//        let h:CGFloat = -1500
        // 2. 修改约束数值
        // 使用自动布局，苹果提供了一个自动布局系统，在后台维护界面元素的位置和大小
        // 一旦使用了自动布局，就不要在直接设置 frame
        // 自动布局系统，会`收集`界面上所有需要重新调正位置/大小的控件的约束，然后一次性修改
        // 如果开发中需要强行更新约束，可以直接调用 layoutIfNeeded 方法，会将当前所有的约束变化应用到控件上
        iconBottomCons?.constant = h
        
        //开始动画
        lable.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 如果需要更新布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.lable.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //UIApplication.sharedApplication().keyWindow?.rootViewController = LSMainViewController()

                        //发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(LSSwitchRootViewControllerNotification, object: nil)
                        
                })
        }
        
        
    }

    /// 设置控件
    private func setupUI() {
        //添加控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(lable)
        
        //设置布局
        backgroundImageView.ff_Fill(view)
        
//        let cons = iconView.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -200))
//        self.iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -200))
        self.iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
//
        
        lable.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 20))
        
    }
    

    //MARK：-懒加载控件
    /// 背景视图
    private lazy var backgroundImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
/// 头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
        }()
/// lable
    private lazy var lable:UILabel = UILabel(title: "欢迎回来", color: UIColor.darkGrayColor(), fontSize: 18)

}
